using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using CymbergajTouch.Components;
using CymbergajTouch.Match;
using CymbergajTouch.Menu;
using CymbergajTouch.Logic;

namespace CymbergajTouch
{
    /// <summary>
    /// Main XNA game class.
    /// </summary>
    public class Cymbergaj : Microsoft.Xna.Framework.Game
    {
        //XNA
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Keyboard
        private KeyboardInput keyboardInput = new KeyboardInput();

        //Menu
        private MenuComponent menuComponent;
        private MenuComponent optionsComponent;

        //Game options
        private Options options = new Options(true, true, GoalsToWin.Seven);

        /// <summary>
        /// Constructor.
        /// </summary>
        public Cymbergaj()
        {
            graphics = new GraphicsDeviceManager(this);
            //graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = IceRink.iceWidth;
            graphics.PreferredBackBufferHeight = IceRink.iceHeight;

            //Menu
            menuComponent = new MenuComponent(this, keyboardInput);
            menuComponent.AddItem(new MenuItem("Play now", BeginMatch));
            menuComponent.AddItem(new MenuItem("Options", ShowOptions));
            menuComponent.AddItem(new MenuItem("Exit", Exit));

            //Options
            options = new Options(true, true, GoalsToWin.Seven);
            optionsComponent = new OptionsComponent(this, keyboardInput, options);
        }

        protected override void Initialize()
        {
            IsFixedTimeStep = true;
            IsMouseVisible = true;
            TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 1);

            Components.Add(menuComponent);
            menuComponent.Enabled = true;

            InputManager.Instance.InitializeInput(this.Window.Handle);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            keyboardInput.Update();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            base.Draw(gameTime);
        }

        public SpriteBatch SpriteBatch
        {
            get { return this.spriteBatch; }
        }

        /// <summary>
        /// Shows main menu.
        /// </summary>
        public void ShowMenu()
        {
            ClearComponents();
            Components.Add(menuComponent);
            menuComponent.Enabled = true;
        }

        /// <summary>
        /// Begins Cymbergaj match.
        /// </summary>
        public void BeginMatch()
        {
            ClearComponents();
            Components.Add(new MatchComponent(this, keyboardInput, options));
        }

        /// <summary>
        /// Shows option menu.
        /// </summary>
        private void ShowOptions()
        {
            ClearComponents();
            Components.Add(optionsComponent);
            optionsComponent.Enabled = true;
        }

        private void ClearComponents()
        {
            foreach (GameComponent component in Components)
            {
                if (component is MenuComponent)
                {
                    ((MenuComponent)component).Enabled = false;
                }
            }
            Components.Clear();
        }
    }
}
